﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Domain.Entities;
using Application.UserApp;
using Utility;
using JFnetcore.Models;
using Application.RoleApp;
using Application.RoleMenuApp;
using Application.MenuApp;
using System.Text;
using Application.LogApp;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JFnetcore.Controllers
{
    public class UsersController : BaseController
    {
        private IUserAppService _userAppService;
        private IRoleAppService _roleAppService;
        private IRoleMenuAppService _rolemenuAppService;
        private IMenuAppService _menuAppService;
        private ILogAppService _logAppService;
        public UsersController(IUserAppService userAppService,
            IRoleAppService roleAppService,
            IRoleMenuAppService rolemenuAppService,
            ILogAppService logAppService,
            IMenuAppService menuAppService)
        {
            _userAppService = userAppService;
            _roleAppService = roleAppService;
            _rolemenuAppService = rolemenuAppService;
            _menuAppService = menuAppService;
            _logAppService = logAppService;
        }

        #region 个人中心

        #region 修改密码
        /// <summary>
        /// 打开密码修改视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Password()
        {
            SessionModel model = getUserSession();
            ViewBag.id = model.User.Id.ToString();
            ViewBag.username = model.User.User_name;
            ViewBag.loginname = model.User.Login_name;
            return View();
        }

        /// <summary>
        /// 表单提交 修改密码接口
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ChangePassword(PasswordModel model)
        {
            User u = getUserSession().User;
            if (u.Id.ToString() != model.UserId)
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(300, "修改密码失败，信息传递错误！", "", "", "", false, "", ""));
            }
            else
            {
                if (u.User_pwd != model.OldPassword)
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "修改密码失败，原密码错误！", "", "", "", false, "", ""));
                }
                else
                {
                    u.User_pwd = model.NewPassword;
                    u.Createtime = DateTime.Now;
                    if (_userAppService.AddOrUpdate(u))
                    {
                        HttpContext.Session.Remove("CurrentUser");

                        List<RoleMenu> list = _rolemenuAppService.GetAll(u.Role_id.ToString());
                        SessionModel smodel = new SessionModel();
                        smodel.IP = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                        smodel.User = u;
                        smodel.RolemenuList = list;
                        HttpContext.Session.Set("CurrentUser", ByteConvertHelper.Object2Bytes(smodel));
                        SessionModel sm = getUserSession();
                        _logAppService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)EnumUtil.OperationEnum.update, "修改密码");
                        return Content(ReturnJsonHelp.BJUIajaxJson(200, "修改密码成功！", "", "", "", true, "", ""));
                    }
                    else
                    {
                        return Content(ReturnJsonHelp.BJUIajaxJson(300, "修改密码失败！", "", "", "", false, "", ""));
                    }
                }
            }
        }
        #endregion

        #region 个人记录
        /// <summary>
        /// 打开个人记录视图
        /// </summary>
        /// <param name="pagemodel"></param>
        /// <returns></returns>
        public IActionResult Myloglist(PageModel pagemodel)
        {
            //获取Session信息并且返回前台
            SessionModel userSession = getUserSession();

            if (string.IsNullOrEmpty(pagemodel.PageCurrent))
                pagemodel.PageCurrent = "1";
            if (string.IsNullOrEmpty(pagemodel.PageSize))
                pagemodel.PageSize = "30";
            ViewBag.SearchKeyWords = pagemodel.SearchKeyWords;
            ViewBag.PageSize = pagemodel.PageSize;
            ViewBag.PageCurrent = pagemodel.PageCurrent;
            ViewBag.OrderField = pagemodel.OrderField;
            ViewBag.OrderDirection = pagemodel.OrderDirection;
            ViewBag.AllRecords = _logAppService.GetAll(pagemodel.SearchKeyWords, userSession.User.Id).Count;
            ViewBag.Modellist = _logAppService.GetPageList(userSession.User.Id, pagemodel.SearchKeyWords, int.Parse(pagemodel.PageSize), int.Parse(pagemodel.PageCurrent), pagemodel.OrderField, pagemodel.OrderDirection);
            return View();

        }
        #endregion

        #endregion

        #region 用户中心

        #region 用户管理
        /// <summary>
        /// 打开用户视图，用户列表
        /// </summary>
        /// <param name="pagemodel"></param>
        /// <returns></returns>
        public IActionResult Index(PageModel pagemodel)
        {
            //获取Session信息并且返回前台
            SessionModel userSession = getUserSession();

            ViewBag.myid = userSession.User.Id.ToString();
            if (string.IsNullOrEmpty(pagemodel.PageCurrent))
                pagemodel.PageCurrent = "1";
            if (string.IsNullOrEmpty(pagemodel.PageSize))
                pagemodel.PageSize = "30";
            ViewBag.SearchKeyWords = pagemodel.SearchKeyWords;
            ViewBag.PageSize = pagemodel.PageSize;
            ViewBag.PageCurrent = pagemodel.PageCurrent;
            ViewBag.OrderField = pagemodel.OrderField;
            ViewBag.OrderDirection = pagemodel.OrderDirection;
            ViewBag.AllRecords = _userAppService.GetAll(pagemodel.SearchKeyWords).Count;
            //ViewBag.Userslist = _userAppService.GetPageList(pagemodel.SearchKeyWords, int.Parse(pagemodel.PageSize), int.Parse(pagemodel.PageCurrent), pagemodel.OrderField, pagemodel.OrderDirection);

            List<User> userlist = _userAppService.GetPageList(pagemodel.SearchKeyWords, int.Parse(pagemodel.PageSize), int.Parse(pagemodel.PageCurrent), pagemodel.OrderField, pagemodel.OrderDirection);
            List<Role> rolelist = _roleAppService.GetAll("");
            ViewBag.Modellist = new CS.vUserHelp().GetVUserList(userlist, rolelist);
            return View();

        }

        /// <summary>
        /// 打开用户编辑视图，添加修改用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Edit(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return View();
            }
            else
            {
                var model = _userAppService.GetById(Int32.Parse(id));
                return View(model);
            }

        }

        /// <summary>
        ///表单提交用户编辑接口 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("UserEdit")]
        public IActionResult UserEdit(User model)
        {
            User u = _userAppService.GetByLoginname(model.Login_name);
            if (u != null && model.Id == 0)
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败,登录账号已存在！", "", "", "", true, "", ""));
            }
            else
            {
                model.Createtime = DateTime.Now;
                if (_userAppService.AddOrUpdate(model))
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(200, "提交成功！", "UsersIndex", "", "", true, "", ""));
                }
                else
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败！", "", "", "", false, "", ""));
                }
            }
        }

        /// <summary>
        /// 按钮提交用户删除接口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult UserDelete(string id)
        {
            if (_userAppService.DeleteById(int.Parse(id)))
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(200, "删除成功！", "usersIndex", "", "", false, "", ""));
            }
            else
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(300, "删除失败！", "", "", "", false, "", ""));
            }
        }

        /// <summary>
        /// 用户编辑视图，加载角色选择视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Search()
        {
            StringBuilder resultstr = new StringBuilder();
            List<Role> list = _roleAppService.GetAll("").Where(i => i.Disable == 1).ToList();
            resultstr.Append("[");
            int y = 0;
            foreach (Role a in list)
            {
                resultstr.Append("{\"Role_id\":\"" + a.Id + "\",\"Role_name\":\"" + a.Role_name + "\"},");
                y++;
            }
            if (y > 0)
                resultstr.Remove(resultstr.ToString().LastIndexOf(','), 1);
            resultstr.Append("]");
            return Content(resultstr.ToString());
        }
        #endregion

        #region 菜单管理
        /// <summary>
        /// 打开菜单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Menu()
        {
            //获取Session信息并且返回前台
            SessionModel userSession = getUserSession();

            StringBuilder menuhtml = new StringBuilder();
            List<Menu> list = _menuAppService.GetAll("");
            List<Menu> list1 = list.Where(i => i.Parent_Id == 0).OrderBy(i => i.Menu_rank).ToList();
            foreach (Menu menu in list1)
            {
                menuhtml.Append("<tr>");
                menuhtml.Append("<td align=\"center\">" + menu.Menu_name + "</td>");
                menuhtml.Append("<td></td>");
                menuhtml.Append("<td></td>");
                menuhtml.Append("<td></td>");
                menuhtml.Append("<td></td>");
                menuhtml.Append("<td></td>");
                menuhtml.Append("<td align=\"center\"><a href=\"/Users/UpdateMenuRank/" + menu.Id + "/-1\" class=\"btn btn-green\" data-toggle=\"doajax\" data-icon=\"arrow-up\"></a> " + menu.Menu_rank + " <a href=\"/Users/UpdateMenuRank/" + menu.Id + "/1\" class=\"btn btn-green\" data-toggle=\"doajax\" data-icon=\"arrow-down\"></a></td>");
                menuhtml.Append("<td></td>");
                menuhtml.Append("<td></td>");
                //menuhtml.Append("<td align =\"center\" >");
                //if (menu.Disable == 1)
                //{
                //    menuhtml.Append("<i class=\"fa fa-check green\"></i>");
                //}
                //else
                //{
                //    menuhtml.Append("<i class=\"fa fa-close red\"></i>");
                //}
                //menuhtml.Append("</td>");
                //menuhtml.Append("<td align =\"center\"><a href =\"Users/MenuEdit/"+menu.Id+"\" class=\"btn btn-green\" data-toggle=\"dialog\" data-width=\"500\" data-height=\"300\" data-id=\"UsersMenuEdit\" data-title=\"编辑 【"+menu.Menu_name+"】\">编辑</a></td>");
                menuhtml.Append("</tr>");
                List<Menu> list2 = list.Where(i => i.Parent_Id == menu.Id).OrderBy(i => i.Menu_rank).ToList();
                foreach (Menu menu2 in list2)
                {
                    menuhtml.Append("<tr>");
                    menuhtml.Append("<td></td>");
                    menuhtml.Append("<td align=\"center\">" + menu2.Menu_name + "</td>");
                    menuhtml.Append("<td></td>");
                    menuhtml.Append("<td></td>");
                    menuhtml.Append("<td></td>");
                    menuhtml.Append("<td></td>");
                    menuhtml.Append("<td></td>");
                    menuhtml.Append("<td align=\"center\"><a href=\"/Users/UpdateMenuRank/" + menu2.Id + "/-1\" class=\"btn btn-green\" data-toggle=\"doajax\" data-icon=\"arrow-up\"></a> " + menu2.Menu_rank + " <a href=\"/Users/UpdateMenuRank/" + menu2.Id + "/1\" class=\"btn btn-green\" data-toggle=\"doajax\" data-icon=\"arrow-down\"></a></td>");
                    menuhtml.Append("<td></td>");
                    //menuhtml.Append("<td align =\"center\" >");
                    //if(menu2.Disable == 1)
                    //    {
                    //    menuhtml.Append("<i class=\"fa fa-check green\"></i>");
                    //}
                    //    else
                    //    {
                    //    menuhtml.Append("<i class=\"fa fa-close red\"></i>");
                    //}
                    //menuhtml.Append("</td>");
                    //menuhtml.Append("<td align =\"center\"><a href =\"Users/MenuEdit/" + menu2.Id + "\" class=\"btn btn-green\" data-toggle=\"dialog\" data-width=\"500\" data-height=\"300\" data-id=\"UsersMenuEdit\" data-title=\"编辑 【" + menu2.Menu_name + "】\">编辑</a></td>");
                    menuhtml.Append("</tr>");
                    List<Menu> list3 = list.Where(i => i.Parent_Id == menu2.Id).OrderBy(i => i.Menu_rank).ToList();
                    foreach (Menu menu3 in list3)
                    {
                        menuhtml.Append("<tr>");
                        menuhtml.Append("<td></td>");
                        menuhtml.Append("<td></td>");
                        menuhtml.Append("<td align=\"center\">" + menu3.Menu_name + "</td>");
                        menuhtml.Append("<td align=\"center\">" + menu3.Menu_idname + "</td>");
                        menuhtml.Append("<td align=\"center\">" + menu3.Menu_url + "</td>");
                        menuhtml.Append("<td align=\"center\">" + menu3.Menu_target + "</td>");
                        menuhtml.Append("<td></td>");
                        menuhtml.Append("<td></td>");
                        menuhtml.Append("<td align=\"center\"><a href=\"/Users/UpdateMenuRank/" + menu3.Id + "/-1\" class=\"btn btn-green\" data-toggle=\"doajax\" data-icon=\"arrow-up\"></a> " + menu3.Menu_rank + " <a href=\"/Users/UpdateMenuRank/" + menu3.Id + "/1\" class=\"btn btn-green\" data-toggle=\"doajax\" data-icon=\"arrow-down\"></a></td>");
                        //menuhtml.Append("<td align =\"center\" >");
                        //if (menu3.Disable == 1)
                        //{
                        //    menuhtml.Append("<i class=\"fa fa-check green\"></i>");
                        //}
                        //else
                        //{
                        //    menuhtml.Append("<i class=\"fa fa-close red\"></i>");
                        //}
                        //menuhtml.Append("</td>");
                        //menuhtml.Append("<td align =\"center\"><a href =\"Users/MenuEdit/" + menu3.Id + "\" class=\"btn btn-green\" data-toggle=\"dialog\" data-width=\"500\" data-height=\"300\" data-id=\"UsersMenuEdit\" data-title=\"编辑 【" + menu3.Menu_name + "】\">编辑</a></td>");
                        menuhtml.Append("</tr>");

                    }
                }
            }
            ViewBag.menuhtml = menuhtml;
            return View();

        }

        /// <summary>
        /// 修改菜单排序
        /// </summary>
        /// <param name="menuid"></param>
        /// <param name="rank"></param>
        /// <returns></returns>
        [Route("Users/UpdateMenuRank/{menuid?}/{rank?}")]
        public IActionResult UpdateMenuRank(int menuid, int rank)
        {
            Menu m = _menuAppService.GetById(menuid);
            m.Menu_rank += rank;
            if (_menuAppService.AddOrUpdate(m))
            {
                SessionModel sm = getUserSession();
                _logAppService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)EnumUtil.OperationEnum.update, "修改菜单顺序 menuid:" + menuid + ",rank:" + rank);
                return Content(ReturnJsonHelp.BJUIajaxJson(200, "提交成功！", "UsersMenu", "", "", false, "", ""));
            }
            else
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败！", "", "", "", false, "", ""));
            }
        }



        /// <summary>
        /// 打开菜单编辑视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult MenuEdit(string id)
        {
            SessionModel userSession = getUserSession();
            if (userSession != null)
            {
                if (string.IsNullOrEmpty(id))
                {
                    return View();
                }
                else
                {
                    var model = _menuAppService.GetById(Int32.Parse(id));
                    return View(model);
                }
            }
            else
            {
                return Redirect("/Login/Index");
            }
        }

        /// <summary>
        ///表单提交菜单编辑接口 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("EditMenu")]
        public IActionResult EditMenu(Menu model)
        {
            model.Createtime = DateTime.Now;
            if (_menuAppService.AddOrUpdate(model))
            {
                SessionModel sm = getUserSession();
                _logAppService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)EnumUtil.OperationEnum.update, "修改菜单名称");
                return Content(ReturnJsonHelp.BJUIajaxJson(200, "提交成功！", "UsersMenu", "", "", true, "", ""));
            }
            else
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败！", "", "", "", false, "", ""));
            }
        }
        #endregion

        #region 角色管理
        /// <summary>
        /// 打开角色视图，角色列表
        /// </summary>
        /// <param name="pagemodel"></param>
        /// <returns></returns>
        public IActionResult Role(PageModel pagemodel)
        {
            SessionModel userSession = getUserSession();

            if (string.IsNullOrEmpty(pagemodel.PageCurrent))
                pagemodel.PageCurrent = "1";
            if (string.IsNullOrEmpty(pagemodel.PageSize))
                pagemodel.PageSize = "30";
            ViewBag.SearchKeyWords = pagemodel.SearchKeyWords;
            ViewBag.PageSize = pagemodel.PageSize;
            ViewBag.PageCurrent = pagemodel.PageCurrent;
            ViewBag.OrderField = pagemodel.OrderField;
            ViewBag.OrderDirection = pagemodel.OrderDirection;
            ViewBag.AllRecords = _roleAppService.GetAll(pagemodel.SearchKeyWords).Count;
            ViewBag.Userslist = _roleAppService.GetPageList(pagemodel.SearchKeyWords, int.Parse(pagemodel.PageSize), int.Parse(pagemodel.PageCurrent), pagemodel.OrderField, pagemodel.OrderDirection);
            return View();

        }

        /// <summary>
        /// 打开角色编辑视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult RoleEdit(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return View();
            }
            else
            {
                var model = _roleAppService.GetById(Int32.Parse(id));
                return View(model);
            }

        }

        /// <summary>
        ///表单提交角色编辑接口 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("EditRole")]
        public IActionResult EditRole(Role model)
        {
            if (model.Id == 1)
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败,管理员禁止修改！", "", "", "", false, "", ""));
            }
            else
            {
                model.Createtime = DateTime.Now;
                model.Disable = 1;
                if (_roleAppService.AddOrUpdate(model))
                {
                    SessionModel sm = getUserSession();
                    _logAppService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)EnumUtil.OperationEnum.update, "修改角色信息 " + model.Role_name);
                    return Content(ReturnJsonHelp.BJUIajaxJson(200, "提交成功！", "UsersRole", "", "", true, "", ""));
                }
                else
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "提交失败！", "", "", "", false, "", ""));
                }
            }

        }

        /// <summary>
        /// 打开角色权限视图
        /// </summary>
        /// <param name="id">roleId</param>
        /// <returns></returns>
        public IActionResult RoleMenu(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                StringBuilder menuhtml = new StringBuilder();
                List<RoleMenu> meullist = _rolemenuAppService.GetAll(id);
                List<Menu> rootlist = _menuAppService.GetAll("0").Where(i => i.Disable == 1).OrderBy(i => i.Menu_rank).ToList();//获取根目录[可用]菜单,并排序
                //ViewBag.menuhtml = GetMenuHtml(1, rootlist, meullist);
                foreach (Menu menu in rootlist)
                {
                    menuhtml.Append("<tr>");
                    menuhtml.Append("<td align=\"center\">" + menu.Menu_name + "</td>");
                    menuhtml.Append("<td></td>");
                    menuhtml.Append("<td></td>");
                    menuhtml.Append(GetDisableHtml(meullist, menu.Id));
                    menuhtml.Append("<td align=\"center\"><a style=\"width: 80px;\" href=\"/Users/OpenRoleMenu/" + id + "/" + menu.Id.ToString() + "/3/1\" class=\"btn btn-green\" data-toggle=\"doajax\" data-icon=\"check\" data-confirm-msg=\"确定要开通【" + menu.Menu_name + "】所有权限吗？\"></a> <a  style=\"width: 80px;\" href=\"/Users/OpenRoleMenu/" + id + "/" + menu.Id.ToString() + "/3/0\" class=\"btn btn-red\" data-toggle=\"doajax\" data-icon=\"close\" data-confirm-msg=\"确定要关闭【" + menu.Menu_name + "】所有权限吗？\"></a></td>");
                    menuhtml.Append("</tr>");
                    List<Menu> rootlist2 = _menuAppService.GetAll(menu.Id.ToString()).Where(i => i.Disable == 1).OrderBy(i => i.Menu_rank).ToList();
                    foreach (Menu menu2 in rootlist2)
                    {
                        menuhtml.Append("<tr>");
                        menuhtml.Append("<td></td>");
                        menuhtml.Append("<td align=\"center\">" + menu2.Menu_name + "</td>");
                        menuhtml.Append("<td></td>");
                        menuhtml.Append(GetDisableHtml(meullist, menu2.Id));
                        menuhtml.Append("<td align=\"center\"><a style=\"width: 60px;\" href=\"/Users/OpenRoleMenu/" + id + "/" + menu2.Id.ToString() + "/2/1\" class=\"btn btn-green\" data-toggle=\"doajax\" data-icon=\"check\" data-confirm-msg=\"确定要开通【" + menu2.Menu_name + "】所有权限吗？\"></a> <a style=\"width: 60px;\" href=\"/Users/OpenRoleMenu/" + id + "/" + menu2.Id.ToString() + "/2/0\" class=\"btn btn-red\" data-toggle=\"doajax\" data-icon=\"close\" data-confirm-msg=\"确定要关闭【" + menu.Menu_name + "】所有权限吗？\"></a></td>");
                        menuhtml.Append("</tr>");
                        List<Menu> rootlist3 = _menuAppService.GetAll(menu2.Id.ToString()).Where(i => i.Disable == 1).OrderBy(i => i.Menu_rank).ToList();
                        foreach (Menu menu3 in rootlist3)
                        {
                            menuhtml.Append("<tr>");
                            menuhtml.Append("<td></td>");
                            menuhtml.Append("<td></td>");
                            menuhtml.Append("<td align=\"center\">" + menu3.Menu_name + "</td>");
                            menuhtml.Append(GetDisableHtml(meullist, menu3.Id));
                            menuhtml.Append("<td align=\"center\"><a href=\"/Users/OpenRoleMenu/" + id + "/" + menu3.Id.ToString() + "/1/1\" class=\"btn btn-green\" data-toggle=\"doajax\" data-icon=\"check\"></a> <a href=\"/Users/OpenRoleMenu/" + id + "/" + menu3.Id.ToString() + "/1/0\" class=\"btn btn-red\" data-toggle=\"doajax\" data-icon=\"close\"></a></td>");
                            menuhtml.Append("</tr>");

                        }
                    }
                }
                ViewBag.menuhtml = menuhtml;
            }
            return View();

        }

        /// <summary>
        /// 根据menuid获取 用户权限
        /// </summary>
        /// <param name="meullist"></param>
        /// <param name="menuId"></param>
        /// <returns></returns>
        private string GetDisableHtml(List<RoleMenu> rolemeullist, int menuId)
        {

            if (rolemeullist.Where(i => i.Menu_Id == menuId).Count() > 0)
            {
                if (rolemeullist.Where(i => i.Menu_Id == menuId).First().Disable == 1)
                {
                    return "<td align=\"center\"><i class=\"fa fa-check green\"></i></td>";
                }
                else
                {
                    return "<td align=\"center\"><i class=\"fa fa-close red\"></i></td>";
                }
            }
            else
            {
                return "<td></td>";
            }
        }

        /// <summary>
        /// 修改角色权限接口
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="menuid"></param>
        /// <param name="grade"></param>
        /// <returns></returns>
        [Route("Users/OpenRoleMenu/{roleid?}/{menuid?}/{grade?}/{disable?}")]
        public IActionResult OpenRoleMenu(string roleid, string menuid, string grade, string disable)
        {
            int gradeint = Int32.Parse(grade);
            try
            {
                List<Menu> menulist = _menuAppService.GetAll("").Where(i => i.Id == int.Parse(menuid)).ToList();
                foreach (Menu m in menulist)
                {
                    RoleMenu rm = _rolemenuAppService.GetAll("").Where(i => i.Role_Id == Int32.Parse(roleid) && i.Menu_Id == m.Id).First();
                    if (rm != null)
                    {
                        rm.Disable = Int32.Parse(disable);
                        _rolemenuAppService.AddOrUpdate(rm);
                    }
                    if (gradeint-- > 0)
                    {
                        List<Menu> menulist2 = _menuAppService.GetAll(m.Id.ToString());
                        foreach (Menu m2 in menulist2)
                        {
                            RoleMenu rm2 = _rolemenuAppService.GetAll("").Where(i => i.Role_Id == Int32.Parse(roleid) && i.Menu_Id == m2.Id).First();
                            if (rm2 != null)
                            {
                                rm2.Disable = Int32.Parse(disable);
                                _rolemenuAppService.AddOrUpdate(rm2);
                            }
                            if (gradeint-- > 0)
                            {
                                List<Menu> menulist3 = _menuAppService.GetAll(m2.Id.ToString());
                                foreach (Menu m3 in menulist3)
                                {
                                    RoleMenu rm3 = _rolemenuAppService.GetAll("").Where(i => i.Role_Id == Int32.Parse(roleid) && i.Menu_Id == m3.Id).First();
                                    if (rm3 != null)
                                    {
                                        rm3.Disable = Int32.Parse(disable);
                                        _rolemenuAppService.AddOrUpdate(rm3);
                                    }
                                }
                            }
                        }
                    }
                }
                SessionModel sm = getUserSession();
                _logAppService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)EnumUtil.OperationEnum.update, "修改角色信息权限 roleid:" + roleid + ",menuid:" + menuid + ",grade:" + grade + ",disable:" + disable);
                return Content(ReturnJsonHelp.BJUIajaxJson(200, "修改成功！", "UsersRoleMenu", "", "", false, "", ""));
            }
            catch (Exception e)
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(300, "修改失败！" + e.Message, "", "", "", false, "", ""));
            }

        }


        /// <summary>
        /// 重置角色所有权限，新建并关闭所有权限
        /// </summary>
        /// <param name="id">roleid</param>
        /// <returns></returns>
        public IActionResult UpdateRolemenu(string id)
        {
            //删除所有权限
            if (_rolemenuAppService.Delete(id))
            {
                try
                {
                    //新增所有权限数据(false)
                    List<Menu> list = _menuAppService.GetAll("");
                    foreach (Menu m in list)
                    {
                        RoleMenu rmodel = new Domain.Entities.RoleMenu()
                        {
                            Disable = 0,
                            Menu_Id = m.Id,
                            Role_Id = Int32.Parse(id)
                        };
                        _rolemenuAppService.AddOrUpdate(rmodel);
                    }
                    SessionModel sm = getUserSession();
                    _logAppService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)EnumUtil.OperationEnum.update, "重置角色[" + _roleAppService.GetById(Int32.Parse(id)).Role_name + "]所有权限，新建并关闭所有权限");
                    return Content(ReturnJsonHelp.BJUIajaxJson(200, "重置成功！", "", "", "", false, "", ""));
                }
                catch (Exception e)
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "重置失败！" + e.Message, "", "", "", false, "", ""));
                }
            }
            else
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(300, "重置失败！", "", "", "", false, "", ""));
            }

        }

        /// <summary>
        /// 按钮提交角色删除接口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult RoleDelete(string id)
        {
            if (_userAppService.GetAll(Int32.Parse(id)).Count() > 0)
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(300, "删除失败,该角色下有用户！", "", "", "", false, "", ""));
            }
            else
            {
                if (_roleAppService.DeleteById(int.Parse(id)))
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(200, "删除成功！", "UsersRole", "", "", false, "", ""));
                }
                else
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "删除失败！", "", "", "", false, "", ""));
                }
            }
        }

        /// <summary>
        /// 重置管理员所有权限，新建并开启所有权限
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateAdminmenu()
        {
            //删除所有权限
            if (_rolemenuAppService.Delete("1"))
            {
                try
                {
                    //新增所有权限数据(false)
                    List<Menu> list = _menuAppService.GetAll("");
                    foreach (Menu m in list)
                    {
                        RoleMenu rmodel = new Domain.Entities.RoleMenu()
                        {
                            Disable = 1,
                            Menu_Id = m.Id,
                            Role_Id = 1
                        };
                        _rolemenuAppService.AddOrUpdate(rmodel);
                    }
                    SessionModel sm = getUserSession();
                    _logAppService.Add(DateTime.Now, sm.IP, sm.User.Id.ToString(), (int)EnumUtil.OperationEnum.update, "重置管理员所有权限，新建并开启所有权限");
                    return Content(ReturnJsonHelp.BJUIajaxJson(200, "重置成功！", "", "", "", false, "", ""));
                }
                catch (Exception e)
                {
                    return Content(ReturnJsonHelp.BJUIajaxJson(300, "重置失败！" + e.Message, "", "", "", false, "", ""));
                }
            }
            else
            {
                return Content(ReturnJsonHelp.BJUIajaxJson(300, "重置失败！", "", "", "", false, "", ""));
            }

        }
        #endregion

        #region 平台记录
        /// <summary>
        /// 打开平台记录视图
        /// </summary>
        /// <param name="pagemodel"></param>
        /// <returns></returns>
        public IActionResult Loglist(PageModel pagemodel)
        {
            //获取Session信息并且返回前台
            SessionModel userSession = getUserSession();

            if (string.IsNullOrEmpty(pagemodel.PageCurrent))
                pagemodel.PageCurrent = "1";
            if (string.IsNullOrEmpty(pagemodel.PageSize))
                pagemodel.PageSize = "30";
            ViewBag.SearchKeyWords = pagemodel.SearchKeyWords;
            ViewBag.PageSize = pagemodel.PageSize;
            ViewBag.PageCurrent = pagemodel.PageCurrent;
            ViewBag.OrderField = pagemodel.OrderField;
            ViewBag.OrderDirection = pagemodel.OrderDirection;
            ViewBag.AllRecords = _logAppService.GetAll(pagemodel.SearchKeyWords).Count;
            //ViewBag.Modellist = _logAppService.GetPageList(0,pagemodel.SearchKeyWords, int.Parse(pagemodel.PageSize), int.Parse(pagemodel.PageCurrent), pagemodel.OrderField, pagemodel.OrderDirection);

            List<Log> Loglist = _logAppService.GetPageList(0, pagemodel.SearchKeyWords, int.Parse(pagemodel.PageSize), int.Parse(pagemodel.PageCurrent), pagemodel.OrderField, pagemodel.OrderDirection);
            List<User> Userlist = _userAppService.GetAll("");
            ViewBag.Modellist = new CS.vLogHelp().GetVLogList(Loglist, Userlist);
            return View();

        }

        #endregion

        #endregion
    }
}
